﻿#include "cameradlg.h"
#include "ui_cameradlg.h"
#include <QRegExp>
#include <QValidator>
#include <QTimer>
#include <QDebug>
CameraDlg::CameraDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::CameraDlg)
{
    ui->setupUi(this);
    this->move(0,58);
    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowMinimizeButtonHint);

    m_nCameraDlgInfo.m_nFunction = 0;
    m_nCameraDlgInfo.m_bOpen = false;
    m_nCameraDlgInfo.m_nCamera = 0;
    m_nCameraDlgInfo.m_nConsecutive = 0;
    m_nCameraDlgInfo.m_nPlate = 0;
    m_nCameraDlgInfo.m_nSingle = 0;
    //2056 2464
    QRegExp regx("[0-0]|[1-9][0-9][0-9]{0,2}");
    QValidator *validator = new QRegExpValidator(regx, this );
    ui->lineEdit_6->setPlaceholderText("0-9999");
    ui->lineEdit_7->setPlaceholderText("0-9999");
    ui->lineEdit_8->setPlaceholderText("0-9999");
    ui->lineEdit_9->setPlaceholderText("0-2056");
    ui->lineEdit_10->setPlaceholderText("0-2464");

    ui->lineEdit_6->setValidator( validator );
    ui->lineEdit_7->setValidator( validator );
    ui->lineEdit_8->setValidator( validator );
    ui->lineEdit_9->setValidator( validator );
    ui->lineEdit_10->setValidator( validator );



    //创建定时器
    singleTimer = new QTimer(this);
    //将定时器超时信号与槽(功能函数)联系起来
    connect(singleTimer, SIGNAL(timeout()), this, SLOT(singleFunction()));

}

CameraDlg::~CameraDlg()
{
    delete ui;
}

void CameraDlg::on_switchBt_1_clicked()
{
    emit changeDlg(1);
}

void CameraDlg::on_switchBt_2_clicked()
{
    emit changeDlg(2);
}

void CameraDlg::on_switchBt_3_clicked()
{
    emit changeDlg(3);
}

void CameraDlg::on_switchBt_4_clicked()
{
    emit changeDlg(4);
}

void CameraDlg::on_camear_1_clicked()
{
    CameraList(1);
}

void CameraDlg::on_camear_2_clicked()
{
    CameraList(2);
}

void CameraDlg::on_camear_3_clicked()
{
    CameraList(3);
}

void CameraDlg::on_camear_4_clicked()
{
    CameraList(4);
}
void CameraDlg::CameraList(int nIndex)
{
    HalconOpenwindows();
    if(!m_nCameraDlgInfo.m_nConsecutive){
        switch (nIndex) {
        case 0:
            ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1A.png);");
            ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2A.png);");
            ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3A.png);");
            ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4A.png);");
            break;
        case 1:
            ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1B.png);");
            ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2A.png);");
            ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3A.png);");
            ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4A.png);");

            m_nCameraDlgInfo.m_nCamera = 1;
            break;
        case 2:
            ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2B.png);");
            ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1A.png);");
            ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3A.png);");
            ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4A.png);");

            m_nCameraDlgInfo.m_nCamera = 2;
            break;
        case 3:
            ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3B.png);");
            ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1A.png);");
            ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2A.png);");
            ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4A.png);");

            m_nCameraDlgInfo.m_nCamera = 3;
            break;
        case 4:
            ui->camear_4->setStyleSheet("border-image: url(:/png/imge/camera/camera_4B.png);");
            ui->camear_1->setStyleSheet("border-image: url(:/png/imge/camera/camera_1A.png);");
            ui->camear_2->setStyleSheet("border-image: url(:/png/imge/camera/camera_2A.png);");
            ui->camear_3->setStyleSheet("border-image: url(:/png/imge/camera/camera_3A.png);");

            m_nCameraDlgInfo.m_nCamera = 4;
            break;
        default:
            m_nCameraDlgInfo.m_nCamera = 0;
            break;
        }
        m_nCameraDlgInfo.m_nFunction = 1;
        emit sigCameraDlgInfo(m_nCameraDlgInfo);
    }

}

void CameraDlg::on_singleBt_clicked()
{
    HalconOpenwindows();
    if(!m_nCameraDlgInfo.m_nCamera || !m_nCameraDlgInfo.m_bOpen || m_nCameraDlgInfo.m_nConsecutive)
    {
        QMessageBox::information(this, tr("Error"), QString::fromLocal8Bit("参数选择错误！"));
        return;
    }
    if(m_nCameraDlgInfo.m_nSingle)
    {
        ui->singleBt->setStyleSheet("border-image: url(:/png/imge/camera/camera_3_1A.png);");
    }else{
        ui->singleBt->setStyleSheet("border-image: url(:/png/imge/camera/camera_3_1B.png);");
    }
    m_nCameraDlgInfo.m_nSingle = 1 - m_nCameraDlgInfo.m_nSingle;
    m_nCameraDlgInfo.m_nFunction = 2;
    emit sigCameraDlgInfo(m_nCameraDlgInfo);
}

void CameraDlg::on_consecutiveBt_clicked()
{
    HalconOpenwindows();
    if(!m_nCameraDlgInfo.m_nCamera || !m_nCameraDlgInfo.m_bOpen || m_nCameraDlgInfo.m_nSingle)
    {
        QMessageBox::information(this, tr("Error"), QString::fromLocal8Bit("参数选择错误！"));
        return;
    }
    if(m_nCameraDlgInfo.m_nConsecutive)
    {
        ui->consecutiveBt->setStyleSheet("border-image: url(:/png/imge/camera/camera_3_2A.png);");
    }else{
        ui->consecutiveBt->setStyleSheet("border-image: url(:/png/imge/camera/camera_3_2B.png);");
    }
    m_nCameraDlgInfo.m_nConsecutive = 1 - m_nCameraDlgInfo.m_nConsecutive;
    m_nCameraDlgInfo.m_nFunction = 3;
    emit sigCameraDlgInfo(m_nCameraDlgInfo);
}

void CameraDlg::HalconOpenwindows()
{
    if (!m_nCameraDlgInfo.m_bOpen)
    {
        Hlong hWind = ui->label->winId();
        HalconCpp::OpenWindow(0, 0, ui->label->width(), ui->label->height(), hWind, "", "", &m_nCameraDlgInfo.hv_WindowHandle);
        m_nCameraDlgInfo.m_bOpen = true;
    }
}

void CameraDlg::singleFunction()
{
	m_nCameraDlgInfo.m_nSingle = 1- m_nCameraDlgInfo.m_nSingle;
	m_nCameraDlgInfo.m_nFunction = 2;
	emit sigCameraDlgInfo(m_nCameraDlgInfo);
}

void CameraDlg::on_plateBt_clicked()
{
    if(m_nCameraDlgInfo.m_nPlate)
    {
        ui->plateBt->setStyleSheet("border-image: url(:/png/imge/camera/camera_2_1A.png);");
    }else{
        ui->plateBt->setStyleSheet("border-image: url(:/png/imge/camera/camera_2_1B.png);");
        HalconOpenwindows();
    }
    m_nCameraDlgInfo.m_nPlate = 1 - m_nCameraDlgInfo.m_nPlate;
}

void CameraDlg::on_saveBt_clicked()
{
    QString input6 = ui->lineEdit_6->text();
    QString input7 = ui->lineEdit_7->text();
    QString input8 = ui->lineEdit_8->text();
    QString input9 = ui->lineEdit_9->text();
    QString input10 = ui->lineEdit_10->text();
    if ( input6.isEmpty()||input7.isEmpty()||input8.isEmpty()
         ||input9.isEmpty()||input10.isEmpty() )
    {
        //输入为空，警告处理
        QMessageBox::question(this, tr("Error"), tr("Input error!"));
        return;
    }
    CameraDlgInfo m_NewData;
    m_NewData.m_nParameter.ExposureTimeAbs = input6.toDouble();
    m_NewData.m_nParameter.OffsetX = input7.toInt();
    m_NewData.m_nParameter.OffsetY = input8.toInt();
    m_NewData.m_nParameter.Height = input9.toInt();
    m_NewData.m_nParameter.Width = input10.toInt();
    if(0 == memcmp(&m_NewData.m_nParameter,&m_nCameraDlgInfo.m_nParameter,sizeof(m_nCameraDlgInfo.m_nParameter))){
        return;
    }
    m_nCameraDlgInfo.m_nParameter = m_NewData.m_nParameter;
    m_nCameraDlgInfo.m_nFunction = 4;
    emit sigCameraDlgInfo(m_nCameraDlgInfo);
}

void CameraDlg::on_continueSingle_clicked()
{
    HalconOpenwindows();
    if(!m_nCameraDlgInfo.m_nCamera || !m_nCameraDlgInfo.m_bOpen || m_nCameraDlgInfo.m_nConsecutive)
    {
        QMessageBox::information(this, tr("Error"), QString::fromLocal8Bit("参数选择错误！"));
        return;
    }
    if(m_nSingle){
        //停止运行定时器
        if (singleTimer->isActive()){
            singleTimer->stop();
        }
        ui->continueSingle->setText(QString::fromLocal8Bit("开启连续触发"));
    }else{
        //开始运行定时器，定时时间间隔为1000ms
        singleTimer->start(100);
        ui->continueSingle->setText(QString::fromLocal8Bit("停止连续触发"));
    }
    m_nSingle = 1- m_nSingle;
}
